﻿/**************************************************************************
创建日期:	2019/7/13 22:39:08     
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com
创建时间:	
Copyright (c)    ZhCun.cn

描	述：
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using ZhCun.WebCommon.AL;
using ZhCun.WebCommon.DBModels;

namespace ZhCun.WebCommon.WebPage
{
    [LoginAuthorize(IsWebApi = false)]
    public class PageAuthorizeController : PageController, IAuthorizeController
    {
        TSysLoginToken _LoginToken;

        /// <summary>
        /// 当前登录用户对象,改对象永不为null，访问是如果为null则初始化，且赋值 LoginHost
        /// </summary>
        public TSysLoginToken LoginToken
        {
            get
            {
                if (_LoginToken == null)
                {
                    _LoginToken = HttpContext.GetTokenObject();
                    if (_LoginToken == null)
                    {
                        _LoginToken = new TSysLoginToken();
                    }
                    _LoginToken.LoginHost = GetRemoteIp();
                }
                return _LoginToken;
            }
        }

        protected override TAL CreateALObject<TAL>()
        {
            var al = base.CreateALObject<TAL>();
            al.SetLoginState(LoginToken);
            return al;
        }
    }

    public class PageAuthorizeController<TAL> : PageAuthorizeController
        where TAL : BaseAL, new()
    {
        TAL _AL;
        /// <summary>
        /// 当前控制器所依赖的AL对象
        /// </summary>
        public TAL ALObj
        {
            get
            {
                if (_AL == null)
                {
                    _AL = CreateALObject<TAL>();
                }
                return _AL;
            }
        }
    }
}
